package sgi.client;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.JTable;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;

import sgi.client.SGIUserAccountMenuController;
import sgi.client.SGIWatchPurchasedImagesController.DataLayer;
import sgi.details_new.ImageDetails;
import sgi.misc.FloatDimension;
import java.awt.Color;
import javax.swing.ImageIcon;
import java.awt.Font;

public class SGIWatchPurchasedImagesUI extends SGIClientFrame {

	private JPanel contentPane;
	private JTable table;
	private JScrollPane scrollPane;
	private static SGIWatchPurchasedImagesController watchController;

	public SGIWatchPurchasedImagesUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 685, 458);
		contentPane = new JPanel();
		contentPane.setLayout(null);
		setContentPane(contentPane);
		setTitle("Watch purchased images - " + SGIUserAccountMenuController.getUser().getUsername());

		
		Object columnNames[] = {"Type", "Image ID", "Location", "Date"};
		Object rowData[][] = {};
		DefaultTableModel dtm = new DefaultTableModel(rowData, columnNames) {
		      public boolean isCellEditable(int rowIndex, int mColIndex) {
		          return false;
		        }
		      };
		
		      
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 27, 649, 331);
		contentPane.add(scrollPane);
	//	scrollPane.setViewportView(table);
		table = new JTable(dtm);
		table.setBounds(10, 27, 649, 331);
		//table.setAutoCreateRowSorter(true);


		//table.getRowSorter().toggleSortOrder(2);	//makes trouble with row order

	
		JTableHeader header = table.getTableHeader();
		JPanel panel = new JPanel();
		scrollPane.setViewportView(panel);
		panel.setLayout(new BorderLayout());
		panel.add(header, BorderLayout.NORTH);
		panel.add(table, BorderLayout.CENTER);
		
		JButton btnNewButton = new JButton("View");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(table.getSelectedRow() > -1)
					watchController.viewImage(new ImageDetails((String) table.getValueAt(table.getSelectedRow(),0),Integer.valueOf((String) table.getValueAt(table.getSelectedRow(),1)),(String) table.getValueAt(table.getSelectedRow(),3),(String) table.getValueAt(table.getSelectedRow(),2),new FloatDimension()));
				
			}
		});
		btnNewButton.setBounds(238, 369, 181, 40);
		contentPane.add(btnNewButton);
		
		JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SGIWatchPurchasedImagesController.terminate();
				SGIUserAccountMenuController.create(null);
			}
		});
		btnBack.setBounds(541, 369, 118, 40);
		contentPane.add(btnBack);
		
		JButton btnSequence = new JButton("Sequence");
		btnSequence.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				watchController.imageSequence();
			}
		});
		btnSequence.setBounds(10, 369, 118, 40);
		contentPane.add(btnSequence);
	}
	
	public void addRow (ImageDetails img)
	{
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		dtm.insertRow(table.getRowCount(), new Object[] {img.getType(),String.valueOf(img.getImageid()),img.getLocation(),img.getDate()});
		table.setModel(dtm);
	}
	
	public void clearTable()
	{
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		while(dtm.getRowCount() > 0) {dtm.removeRow(0);}
	}

	public static SGIWatchPurchasedImagesController getWatchController() {
		return watchController;
	}

	public static void setController(SGIWatchPurchasedImagesController watchController) {
		SGIWatchPurchasedImagesUI.watchController = watchController;
	}
	
	public boolean findDataLayer(int imageid,DataLayer d)
	{
		for(int i=table.getRowCount();i<0;i++)
		{
			if(((int)table.getValueAt(i, 1)) == imageid)
			{
				if(((String)table.getValueAt(i, 0)).equals(d.toString()));
				return true;
			}
		}
		
		return false;
	}
}
